﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using System.Text.RegularExpressions;

namespace Meraz.Core.Parsers
{
    public class DailyKosParser : RegexBlogParser
    {
        public DailyKosParser()
        {
            this.BlogKey = BlogKeyType.DailyKos;
            this.LinksToIgnoreList = new List<string>();
        }
        internal override List<string> ParseAliases(string source)
        {
            //<h3 class="byline">by <a href="http://darksyde.dailykos.com">DarkSyde</a> </h3>
            string startMarker = @"<h3 class=""byline"">by ";
            int startIndex = source.IndexOf(startMarker);
            if (startIndex < 0)
                throw new ApplicationException("No byline found for DailyKos");
            startIndex += startMarker.Length;
            int endIndex = source.IndexOf("</h3>", startIndex);
            string byline = source.Substring(startIndex, endIndex - startIndex);
            LinksToIgnoreList.AddRange(ParseLinks(byline));
            return new List<string> {RemoveHtml(byline)};
        }

        internal override DateTime ParseDate(string source)
        {
//            <h4 class="date">Sat Jul 31, 2010 at 02:50:04 PM PDT</h4>
            Regex dateLineRegex = new Regex(@"<h4 class=['""]date['""]>(?<date>[^<]+)</h4>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Match dateLineMatch = dateLineRegex.Match(source);
            if (!dateLineMatch.Success)
                return DateTime.MinValue;

            string dateString = dateLineMatch.Groups["date"].Value;
            Regex dateRegex = new Regex(@"^(?<date>.+) at (?<time>.+) \w\w\w$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match dateMatch = dateRegex.Match(dateString.Trim());
            if (dateMatch.Success)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(dateMatch.Groups["date"].Value);
                sb.Append(' ');
                sb.Append(dateMatch.Groups["time"].Value);
                DateTime result;
                if (DateTime.TryParse(sb.ToString(), out result))
                    return result;
            }
            return DateTime.MinValue;
        }

        internal override string ParseBody(string source)
        {
            string startMarker = @"<div class=""entry"">";
            string endMarker = @"<div id=""extended"">";
            string removeStartMarker =@"<div id=""sharing""";
            string removeEndMarker =@"<h4 class=""date"">";
            int startIndex = 0, endIndex = 0, startRemove = 0, endRemove = 0;
            startIndex = source.IndexOf(startMarker);
            endIndex = source.IndexOf(endMarker, startIndex);
            if (0 < startIndex && startIndex < endIndex)
            {
                string story = source.Substring(startIndex, endIndex - startIndex).Trim();
                startRemove = story.IndexOf(removeStartMarker);
                endRemove = story.IndexOf(removeEndMarker);
                if (0 < startRemove && startRemove < endRemove)
                    return story.Remove(startRemove, endRemove - startRemove);
                else
                    return story;
            }
            else
                return source;
        }

        internal override void CleanUp(PostValues post)
        {
        }
    }
}
